const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const uploadImage = (url, token, success) => {
  var key = Math.random().toString(36).substr(2);
  wx.uploadFile({
    filePath: url,
    header: {
      "Content-Type": "multipart/form-data",
      'accept': 'application/json',
      'Authorization': token //若有token，此处换上你的token，没有的话省略
    },
    name: 'file',
    formData: {
      'key': key //这里是为文件设置上传后的文件名
    },
    // 
    url: 'https://xiaochengxu.printeasy.top/prod-api/common/upload',
    success: function (res) {
      if (res.statusCode != 200) {
        wx.showToast({
          title: '图片保存失败',
          icon: 'none'
        })
        return false;
      }
      let data = JSON.parse(res.data);
      if (data.code != 200) {
        wx.showToast({
          title: '图片保存失败',
          icon: 'none'
        })
        return false;
      }
      if (data.code == 401) {
        wx.navigateTo({
          url: '/pages/login/login',
        })
        return false;
      }
      success.call(this, data);
    },
    fail: function (res) {
      console.log(res);
      wx.showToast({
        title: '文件保存失败',
        icon: 'none'
      })
    }
  })
}
module.exports = {
  formatTime: formatTime,
  uploadImage: uploadImage
}
